package patrol.mobile.com.mobilepatrol.activity.security.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.security.Model;
import patrol.mobile.com.mobilepatrol.bean.CuringCheck3Bean;

/**
 * Created by admin on 2018/9/9.
 */

public class ZHCXEmergencyCuringCheckAdapter extends BaseAdapter {

    private int currentItem = -1;
    List<CuringCheck3Bean.AbkSpecialExaminationItemListBean> abkSpecialExaminationItemListBeanList = new ArrayList<>();
    List<CuringCheck3Bean.AbkSpecialExaminationItemListBean> copyList = new ArrayList<>();
    private List<Boolean> YES = new ArrayList<>();
    private List<Model> rabYesMap = new ArrayList<>();
    private List<Model> rabSitMap = new ArrayList<>();
    private Context context;
    ArrayList<Model> list;

    public ZHCXEmergencyCuringCheckAdapter(Context context, List<CuringCheck3Bean.AbkSpecialExaminationItemListBean> abkSpecialExaminationItemListBeanList) {
        this.context = context;
        this.abkSpecialExaminationItemListBeanList = abkSpecialExaminationItemListBeanList;
        this.copyList.addAll(abkSpecialExaminationItemListBeanList);
        for (int i = 0; i < abkSpecialExaminationItemListBeanList.size(); i++) {
            Model model = new Model();
            if (abkSpecialExaminationItemListBeanList.get(i).getYes().equals("是")) {
                model.setCheckedNum(1);
            } else {
                model.setCheckedNum(2);
            }
            rabYesMap.add(model);

            Model model2 = new Model();
            if (abkSpecialExaminationItemListBeanList.get(i).getSituation().equals("未整改")) {
                model2.setCheckedNum(2);
            } else {
                model2.setCheckedNum(1);
            }

            rabSitMap.add(model2);
        }
    }

    @Override
    public int getCount() {
        return abkSpecialExaminationItemListBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return abkSpecialExaminationItemListBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        //判断是否有缓存
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_zhcx_curing_check3, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            //得到缓存的布局
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_num.setText((position + 1) + "");
        holder.iv_arror.setTag(position);
        holder.re_spand.setTag(position);
        if (currentItem == position) {
            holder.ll_expand.setVisibility(View.VISIBLE);
        } else {
            holder.ll_expand.setVisibility(View.GONE);
        }

        final CuringCheck3Bean.AbkSpecialExaminationItemListBean item = abkSpecialExaminationItemListBeanList.get(position);


        holder.et_resume.setText(item.getResume());
//        holder.et_measure.setText(item.getMeasure());
        holder.rg_zhenggai.clearCheck();
        switch (item.getMeasure()) {
            case "0":
                holder.rg_zhenggai.check(R.id.rb_lj);
                break;
            case "1":
                holder.rg_zhenggai.check(R.id.rb_qx);
                holder.et_day.setText(item.getMday());
                break;
            default:
                break;
        }
        holder.tv_chackPro.setText(item.getChackPro());
        holder.rg_pro.setOnCheckedChangeListener(null);
        holder.rg_sit.setOnCheckedChangeListener(null);
        //2.先清空所有选项
        holder.rg_sit.clearCheck();
        holder.rg_sit.clearCheck();
        switch (rabYesMap.get(position).getCheckedNum()) {
            case 1:
                holder.rg_pro.check(R.id.rb_yes);
                break;
            case 2:
                holder.rg_pro.check(R.id.rb_no);
                break;
            default:
                break;

        }
        switch (rabSitMap.get(position).getCheckedNum()) {
            case 1:
                holder.rg_sit.check(R.id.rb_situationYes);
                break;
            case 2:
                holder.rg_sit.check(R.id.rb_situationNo);
                break;
            default:
                break;
        }

        holder.rg_pro.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int p = 0;
                if (checkedId == R.id.rb_yes) {
                    p = 1;
                    copyList.get(position).setYes("是");
                } else {
                    p = 2;
                    copyList.get(position).setYes("否");
                }

                rabYesMap.get(position).setCheckedNum(p);

            }
        });

        holder.rg_sit.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int p = 0;
                if (checkedId == R.id.rb_situationNo) {
                    p = 2;
                    copyList.get(position).setSituation("未整改");
                } else {
                    p = 1;
                    copyList.get(position).setSituation("已整改");
                }
                rabSitMap.get(position).setCheckedNum(p);
            }

        });
        holder.iv_arror.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                int tag = (int) view.getTag();
                if (tag == currentItem) { //再次点击
                    currentItem = -1; //给 currentItem 一个无效值
                } else {
                    currentItem = tag;
                }
                //通知adapter数据改变需要重新加载
                notifyDataSetChanged(); //必须有的一步
            }
        });
        holder.re_spand.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                int tag = (int) view.getTag();
                if (tag == currentItem) { //再次点击
                    currentItem = -1; //给 currentItem 一个无效值
                } else {
                    currentItem = tag;
                }
                //通知adapter数据改变需要重新加载
                notifyDataSetChanged(); //必须有的一步
            }
        });

        return convertView;
    }

    private final class ViewHolder {
        RelativeLayout re_spand;
        ImageView iv_arror;//图片
        TextView tv_num;//
        TextView tv_chackPro;//
        TextView et_resume;//
        TextView et_measure;//
        RadioGroup rg_pro;//
        RadioGroup rg_zhenggai;//
        RadioButton rb_yes;//
        RadioButton rb_no;//
        RadioGroup rg_sit;//
        RadioButton rb_situationYes;//
        RadioButton rb_situationNo;//
        LinearLayout ll_expand;
        EditText et_day;

        /**
         * 构造器
         *
         * @param view 视图组件（ListView的子项视图）
         */
        ViewHolder(View view) {
            iv_arror = (ImageView) view.findViewById(R.id.iv_arror);
            re_spand = (RelativeLayout) view.findViewById(R.id.re_spand);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            tv_chackPro = (TextView) view.findViewById(R.id.tv_chackPro);
            et_resume = (TextView) view.findViewById(R.id.et_resume);
            et_measure = (TextView) view.findViewById(R.id.et_measure);
            rg_pro = (RadioGroup) view.findViewById(R.id.rg_pro);
            rg_zhenggai = (RadioGroup) view.findViewById(R.id.rg_zhenggai);
            rb_yes = (RadioButton) view.findViewById(R.id.rb_yes);
            rb_no = (RadioButton) view.findViewById(R.id.rb_no);
            rg_sit = (RadioGroup) view.findViewById(R.id.rg_sit);
            rb_situationYes = (RadioButton) view.findViewById(R.id.rb_no);
            rb_situationNo = (RadioButton) view.findViewById(R.id.rb_no);
            ll_expand = (LinearLayout) view.findViewById(R.id.ll_expand);
            et_day = (EditText) view.findViewById(R.id.et_day);

        }
    }

    public interface IDataListaner {
        void doData(String num,
                    String chackPro,
                    String yes,
                    String resume,
                    String measure,
                    String situation,
                    String auditFlag,
                    String auditName
        );

        void doList(List<CuringCheck3Bean.AbkSpecialExaminationItemListBean> copyList);

    }

    public IDataListaner iDataListaner;

    public void setDataListener(IDataListaner iDataListaner) {
        this.iDataListaner = iDataListaner;
    }


    public List<CuringCheck3Bean.AbkSpecialExaminationItemListBean> getCopyList() {
        return copyList;
    }

}
